#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// PandoraFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Pandora;
using UnityEditor;

namespace PandoraEditor
{
    public class AssetObject
    {
        public string path;
        public string name;
        public GameObject gameObject;
        public Texture texture;
        public TextureImporter textureImporter;
        public Sprite sprite;
        public AssetImporter fbxImporter;
        private AssetObject()
        {
        }
        public static AssetObject Create<T>(string path)
        {
            var assetObject = new AssetObject();
            assetObject.path = path.Replace("\\", "/");
            string[] pathSplit = assetObject.path.Split('/');
            assetObject.name = pathSplit[pathSplit.Length - 1];
            if (typeof(T) == typeof(GameObject))
            {
                assetObject.gameObject = AssetDatabase.LoadAssetAtPath<GameObject>(path);
            }
            if (typeof(T) == typeof(Texture))
            {
                assetObject.textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
                assetObject.texture = AssetDatabase.LoadAssetAtPath<Texture>(path);
            }
            if (typeof(T) == typeof(Sprite))
            {
                assetObject.sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
            }
            if (typeof(T) == typeof(AssetImporter))
            {
                assetObject.fbxImporter = AssetImporter.GetAtPath(path);
            }
            return assetObject;
        }
    }

    public class PdrFindBase
    {
        public delegate bool GameObjectDelegate(GameObject go);
        public static List<string> GetPrefabs(string rootPath)
        {
            return PdrFileUtil.GetFilesName(rootPath, ".prefab");
        }
        public static List<string> GetSourceSprites(string rootPath, string[] banDic = null)
        {
            return PdrFileUtil.GetFilesNameExact(rootPath, ".png", banDic);
        }
        public static List<string> GetTextureImporters(string rootPath, string[] banDic = null)
        {
            return PdrFileUtil.GetFilesNameExact(rootPath, ".png;.tga;.psd", banDic);
        }
        public static List<AssetObject> GetAssetTextureImporters(string rootPath, string[] banDic = null)
        { 
            List<AssetObject> result = new List<AssetObject>();
            if (File.Exists(rootPath))
            {
                result.Add(AssetObject.Create<Texture>(rootPath));
                return result;
            }
            foreach (var path in GetTextureImporters(rootPath, banDic))
            {
                result.Add(AssetObject.Create<Texture>(path));
            }
            return result;
        }
        public static List<string> GetFbxImporters(string rootPath)
        {
            return PdrFileUtil.GetFilesNameExact(rootPath, ".fbx");
        }
    }
}